package org.rpgmapper.view;

import org.rpgmapper.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

public class PickerView extends FrameLayout {

	private int value = 12;
	private int min = -50;
	private int max = 500;
	private TextView txtValue;
	private ImageButton lessButton;
	private ImageButton moreButton;
	private boolean fingerIsDown = false;
	private Timer timer;
	private TimerHandler handler;

	private OnPickerValueChangeListener onPickerValueChangeListener;
	
	public PickerView(Context context) {
		super(context);
		init();
	}
	public PickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	public PickerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		LayoutInflater.from(getContext()).inflate(R.layout.picker, this);
		txtValue = (TextView)findViewById(R.id.value);
		lessButton = (ImageButton)findViewById(R.id.less);
		moreButton = (ImageButton)findViewById(R.id.more);
		
		txtValue.setText(""+value);
		lessButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				substract();
			}
		});
		moreButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				add();
			}
		});
		moreButton.setOnTouchListener(onTouchListener);
		moreButton.setLongClickable(true);
		moreButton.setOnLongClickListener(new View.OnLongClickListener() {
			public boolean onLongClick(View v) {
				handler = new TimerHandler();
				timer = new Timer(Timer.WHAT_INC);
				handler.postDelayed(timer, 100);
				return true;
			}
		});
		lessButton.setOnTouchListener(onTouchListener);
		lessButton.setLongClickable(true);
		lessButton.setOnLongClickListener(new View.OnLongClickListener() {
			public boolean onLongClick(View v) {
				handler = new TimerHandler();
				timer = new Timer(Timer.WHAT_DEC);
				handler.postDelayed(timer, 100);
				return true;
			}
		});
	}
	
	private OnTouchListener onTouchListener = new View.OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				fingerIsDown = true;
				break;
			case MotionEvent.ACTION_UP:
				fingerIsDown = false;
				break;
			default:
				break;
			}
			return false;
		}
	};

	private void add() {
		int oldValue = value;
		if (value+1 > max) {
			value = min;
		} else {
			value++;
		}
		txtValue.setText(""+value);
		if (PickerView.this.onPickerValueChangeListener != null) {
			PickerView.this.onPickerValueChangeListener.onValueChange(PickerView.this, oldValue, value);
		}
	}
	
	private void substract() {
		int oldValue = value;
		if (value-1 < min) {
			value = max;
		} else {
			value--;
		}
		txtValue.setText(""+value);
		if (PickerView.this.onPickerValueChangeListener != null) {
			PickerView.this.onPickerValueChangeListener.onValueChange(PickerView.this, oldValue, value);
		}
	}


	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
		txtValue.setText(""+value);
	}
	public int getMin() {
		return min;
	}
	public void setMin(int min) {
		this.min = min;
	}
	public int getMax() {
		return max;
	}
	public void setMax(int max) {
		this.max = max;
	}
	public OnPickerValueChangeListener getOnPickerValueChangeListener() {
		return onPickerValueChangeListener;
	}
	public void setOnPickerValueChangeListener(
			OnPickerValueChangeListener onPickerValueChangeListener) {
		this.onPickerValueChangeListener = onPickerValueChangeListener;
	}


	private class Timer implements Runnable {
		
		private final int INTERVAL = 100; //in ms
		public static final int WHAT_DEC = 1;
		public static final int WHAT_INC = 2;
		private int incrementing = WHAT_DEC;
		
		public Timer(int incrementing) {
			this.incrementing = incrementing;
		}

		public void run() {
			if (fingerIsDown) {
		        handler.postDelayed(this, INTERVAL);
		        handler.sendEmptyMessage(incrementing);
			}
		}

	}
	
	private class TimerHandler extends Handler {
		
	    //private Timer timer;
		
		public TimerHandler() {
			//timer = new Timer(this);
		}

	    public void handleMessage(Message msg) {
	    	if (msg.what == Timer.WHAT_DEC) {
	    		substract();
	    	} else {
	    		add();
	    	}
	    }

	    /*public void startTimers() {
	        this.postDelayed(this.timer, 1000); 
	    }

	    public void stopTimers() {
	        this.removeCallbacks(timer);
	    }*/
	    
	}

}
